﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using ZXing.Common;

public enum TimeUnit {
    //天
    DAYS,
    //时
    HOURS,
    //分
    MINUTES,
    //秒
    SECONDS
}

public class VIPConvertUtil {

    /// <summary>
    /// Int转换Bool
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IntToBool(int value) {
        if (value == 0) return false;
        return true;
    }

    /// <summary>
    /// Bool转Int
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static int BoolToInt(bool value) {
        return value ? 1 : 0;
    }

    /// <summary>
    /// 将Texture2D转换为Byte[]
    /// </summary>
    /// <param name="value"></param>
    public static byte[] Texture2DToBytes(Texture2D value)
    {
        return value.EncodeToPNG();
    }

    /// <summary>
    /// 将Byte[]转换为Texture2D
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Texture2D BytesToTexture2D(byte[] value)
    {
        Texture2D texture2D = new Texture2D(Screen.width, Screen.height);
        texture2D.LoadImage(value);
        return texture2D;
    }

    /// <summary>
    /// 将Byte[]转换为Texture
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Texture BytesToTexture(byte[] value)
    {
        Texture2D texture2D = new Texture2D(Screen.width, Screen.height);
        texture2D.LoadImage(value);
        return texture2D;
    }

    /// <summary>
    /// 将Texture2D转Sprite
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Sprite Texture2DToSprite(Texture2D value)
    {
        Rect rect = new Rect(0, 0, value.width, value.height);
        Sprite sprite = Sprite.Create(value, rect, new Vector2(1, 1));
        return sprite;
    }

    /// <summary>
    /// 将Texture2D转Sprite
    /// </summary>
    /// <param name="value"></param>
    /// <param name="rect"></param>
    /// <returns></returns>
    public static Sprite Texture2DToSprite(Texture2D value, Rect rect)
    {
        Sprite sprite = Sprite.Create(value, rect, new Vector2(1, 1));
        return sprite;
    }

    /// <summary>
    /// Sprite转Texture2D
    /// </summary>
    /// <param name="sprite"></param>
    /// <returns></returns>
    public static Texture2D SpriteToTexture2D(Sprite sprite)
    {
        return sprite.texture;
    }

    /// <summary>
    /// DateTime转时间戳
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    public int DateTimeToStamp(DateTime time)
    {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        return (int)(time - startTime).TotalSeconds;
    }

    /// <summary>
    /// 时间戳转DateTime
    /// </summary>
    /// <param name="stamp"></param>
    /// <returns></returns>
    public static DateTime StampToDateTime(int stamp) {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        DateTime dt = startTime.AddSeconds(stamp);
        return dt;
    }

    /// <summary>
    /// String转DateTime
    /// </summary>
    /// <param name="dateStr">格式：1996-2-13</param>
    /// <returns></returns>
    public static DateTime StringToDateTime(string dateStr) {
        DateTime dt = Convert.ToDateTime(dateStr);
        return dt;
    }

    /// <summary>
    /// DateTime转String
    /// </summary>
    /// <param name="dateStr"></param>
    /// <returns></returns>
    public static string DateTimeToString(DateTime dateTime)
    {
        string dateStr = dateTime.ToString("yyyy-MM-dd");
        return dateStr;
    }

    /// <summary>
    /// 转换为ascii码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static int ASCII(string str)
    {
        byte[] array = new byte[1];
        array = Encoding.ASCII.GetBytes(str);
        int asciicode = (short)(array[0]);
        return asciicode;
    }

    /// <summary>
    /// BitMatrix转Texture2D
    /// </summary>
    /// <param name="bitMatrix"></param>
    /// <returns></returns>
    public static Texture2D BitMatrixToTexture2D(BitMatrix bitMatrix) {
        int w = bitMatrix.Width;
        int h = bitMatrix.Height;
        Texture2D texture = new Texture2D(w, h);
        for (int x = 0; x < h; x++)
        {
            for (int y = 0; y < w; y++)
            {
                if (bitMatrix[x, y])
                {
                    texture.SetPixel(y, x, Color.black);
                }
                else
                {
                    texture.SetPixel(y, x, Color.white);
                }
            }
        }
        return texture;
    }

}
